//
//  QLMDiscussVC.m
//  Xiaomakuaixiu
//
//  Created by 龚碧容 on 2017/11/6.
//  Copyright © 2017年 hwd. All rights reserved.
//

#import "QLMDiscussVC.h"

@interface QLMDiscussVC ()<UITextViewDelegate>

@end

@implementation QLMDiscussVC

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
    self.title=@"咨询留言";
    self.view.backgroundColor=[UIColor whiteColor];
    
    self.scrollTopConstr.constant=0;
    
   _contenTextView.placeholder = @"请在此输入留言内容，我们会尽快与您联系";
   _contenTextView.placeholderColor=[UIColor lightGrayColor];
    
   _nameTextField.text = [[NSUserDefaults standardUserDefaults] objectForKey:kLoginUserName];
   _telphoneTextField.text = [[NSUserDefaults standardUserDefaults] objectForKey:kLoginPhoneName];
}

-(void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
//    self.navigationController.navigationBar.translucent=YES;
}

-(void)viewWillDisappear:(BOOL)animated {
    [super viewWillDisappear:animated];
//    self.navigationController.navigationBar.translucent=NO;
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (IBAction)callPhonenumAction:(UIButton *)sender {
    NSString *telNumberStr = [NSString stringWithFormat:@"tel://%@",@"4008896977"];
    UIWebView *webView =[[UIWebView alloc] initWithFrame:CGRectZero];
    [webView loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:telNumberStr]]];
    [self.view addSubview:webView];
}

#pragma mark - UITextView delegate
//- (BOOL)textViewShouldBeginEditing:(UITextView *)textView{
//    textView.text=nil;
//    return YES;
//}
//
//- (void)textViewDidEndEditing:(UITextView *)textView{
//    if ([textView.text length]==0) {
//        textView.text=@"请在此输入留言内容，我们会尽快与您联系";
//    }
//}

//正在改变
- (void)textViewDidChange:(UITextView *)textView{
    //字数限制操作
    if (textView.text.length >= 200) {
        textView.text = [textView.text substringToIndex:200];
    }
    
    if (!textView.hasText) { // textView.text.length
        _contenTextView.placeholder = @"请在此输入留言内容，我们会尽快与您联系";
        _contenTextView.placeholderColor=[UIColor lightGrayColor];
    }else{
        _contenTextView.placeholder = @"";
    }
}

#pragma mark - UIButton Action
- (IBAction)commitBtnAction:(UIButton *)sender {
    
    [self.view endEditing:YES];
    
    if (_contenTextView.text.length==0||[_contenTextView.text isEqualToString:@"请在此输入留言内容，我们会尽快与您联系"]) {
        ETToast(@"请输入留言内容");
        return;
    }

    if (_nameTextField.text.length==0) {
        ETToast(@"请输入姓名");
        return;
    }
    if (_telphoneTextField.text.length==0) {
        ETToast(@"请输入电话");
        return;
    }
    
    NSMutableDictionary *dic=[NSMutableDictionary dictionary];
    
    [dic setObject:_contenTextView.text forKey:@"content"];
    [dic setObject:_nameTextField.text forKey:@"userName"];
    [dic setObject:_telphoneTextField.text forKey:@"userPhone"];
    
    NSRange range = [_htmlurlStr rangeOfString:@"?"];
    NSString *str2 = [_htmlurlStr substringFromIndex:range.location+1];
    
    NSArray *tipArr = [str2 componentsSeparatedByString:@"&"];
    for (int i=0;i<tipArr.count-1;i++) {
        NSArray *tmpidArr = [tipArr[i] componentsSeparatedByString:@"="];
        if (i==0) {
            _machineId=tmpidArr[1];
        }else{
            _machineType=tmpidArr[1];
        }
    }
    
    [dic setObject:[NSString stringWithFormat:@"%@",_machineType] forKey:@"machineType"];
    [dic setObject:[NSString stringWithFormat:@"%@",_machineId] forKey:@"machineId"];
    
    [dic setObject:@"管理员" forKey:@"targetUserName"];
    [dic setObject:@"0" forKey:@"ownerUserType"];
    [dic setObject:_nameTextField.text forKey:@"ownerUserName"];
    [dic setObject:@"" forKey:@"detailUrl"];
    
    NSString *userId=[[NSUserDefaults standardUserDefaults] objectForKey:kLoginUserId];
    [dic setObject:userId forKey:@"userId"];
    
    [self.view makeToastActivity:CSToastPositionCenter];
    [ClientTool postUrlPathAndFormData:@"/comment/replyComment" withParamers:dic andBody:nil
                    success:^(id responseObject){
                        [self.view hideToastActivity];
                        NSInteger code = [[responseObject objectForKey:@"flag"] integerValue];
                        if (code == 1) {
                            [self.navigationController popViewControllerAnimated:YES];
                        }else{
                            ETToast(responseObject[@"msg"]);
                        }
                    }failure:^(NSError *error){
                        [self.view hideToastActivity];
                        ETToast(@"服务器连接失败");
                    }
     ];
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
